#include <algorithm>
#include <cstdint>
#include <functional>
#include <iostream>
#include <queue>
#include <utility>
#include <vector>

using ll = int64_t;
using std::cin, std::cout;

const ll maxn = 1e5+5;
ll n, m, s, dist[maxn], inf {(1ll<<31)-1};
std::vector<std::pair<ll, ll>> adj[maxn];

int main(){
    std::iostream::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);

    cin>>n>>m>>s;

    std::fill(dist, dist+1+n, inf);
    dist[s]=0;
    for(ll i{1}; i<=m; i++){
        ll u, v, w;
        cin>>u>>v>>w;
        for(auto& edge: adj[u]){
            if(edge.first==v){
                edge.second=std::min(edge.second, w);
                goto end;
            }
        }
        adj[u].emplace_back(v, w);
        end:;
    }

    std::priority_queue<std::pair<ll, ll>, std::vector<std::pair<ll, ll>>, std::greater<>> pq;

    pq.emplace(0, s);

    while(pq.size()){
        auto [d, u] = pq.top();
        pq.pop();

        if(d>dist[u])continue;

        for(auto& edge: adj[u]){
            auto [v, w] {edge};
            if(dist[u] + w < dist[v]){
                dist[v] = dist[u] + w;
                pq.emplace(dist[v], v);
            }
        }
    }

    for(ll i{1}; i<=n; i++){
        cout<<dist[i]<<' ';
    }
    cout<<'\n';
}